package com.zhouyibike.uu.ui.activity.mybike;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.MyBicycleNumberInterfaceResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.activity.mywallet.MyDetailActivity;
import com.zhouyibike.uu.ui.adapter.DancheinfoAdapter;
import com.zhouyibike.uu.ui.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class MybikeinfoActivity extends BaseActivity implements View.OnClickListener {
    private int cansellnum=0;
    private TextView tv_zr,tv_sy,tv_rg;
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private GridView gv;
    private DancheinfoAdapter adapter;
    private LinearLayout ll_mybike_nomsg ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mybikeinfo);
        cansellnum = getIntent().getIntExtra("cansellnum", 0);
        initView();
        EventBus.getDefault().register(this);
        getMsg();
        initListener();
    }

    private void initListener() {
        tv_zr.setOnClickListener(this);
        tv_sy.setOnClickListener(this);
        tv_rg.setOnClickListener(this);
    }

    private void initView() {
        ll_mybike_nomsg= (LinearLayout) findViewById(R.id.ll_mybike_nomsg);
        gv= (GridView) findViewById(R.id.mydanche_gv);
        tv_zr= (TextView) findViewById(R.id.mybike_zr);
        tv_sy= (TextView) findViewById(R.id.mybike_detail);
        tv_rg= (TextView) findViewById(R.id.buydanche);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("单车详情");
    }
    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        addSubscription(apiStores.MyBicycleNumberInterface(params),
                new ApiCallback<MyBicycleNumberInterfaceResult>() {

                    @Override
                    public void onSuccess(MyBicycleNumberInterfaceResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {
                            cansellnum=model.getData().size();
                            if (cansellnum>0)
                            {
                                gv.setVisibility(View.VISIBLE);
                                ll_mybike_nomsg.setVisibility(View.GONE);
                            }else
                            {
                                gv.setVisibility(View.GONE);
                                ll_mybike_nomsg.setVisibility(View.VISIBLE);
                            }
                            adapter=new DancheinfoAdapter(MybikeinfoActivity.this,model.getData());
                            gv.setAdapter(adapter);
                        } else {
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {

                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }
    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.mybike_zr:
                Intent intent1 = new Intent(MybikeinfoActivity.this, ZhuanRangActivity.class);
                intent1.putExtra("zrnum", cansellnum);
                startActivityForResult(intent1, 10010);
            break;
            case R.id.mybike_detail:
                Intent intent2 = new Intent(MybikeinfoActivity.this, MyDetailActivity.class);
                intent2.putExtra("type", "2");
                startActivity(intent2);
                break;
            case R.id.buydanche:
                Intent intent = new Intent(MybikeinfoActivity.this, RengouActivity.class);
                startActivityForResult(intent, 100);
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 1) {
            getMsg();
        } else if (requestCode == 10010 && resultCode == 1) {
            setResult(1);
            finish();
        }
        if (resultCode==789)
        {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void addbike(String result) {
        if (result.equals("buybikeok")) {
            getMsg();
        }
    }
}
